using System.Linq;
using Abp.Application.Editions;
using Abp.Application.Features;
using Abp.ZeroCore.SampleApp.Application;
using Abp.ZeroCore.SampleApp.Core;

namespace Abp.ZeroCore.SampleApp.EntityFramework.Seed.Host;

public class DefaultEditionCreator
{
    private readonly SampleAppDbContext _context;

    public DefaultEditionCreator(SampleAppDbContext context)
    {
        _context = context;
    }

    public void Create()
    {
        CreateEditions();
    }

    private void CreateEditions()
    {
        var defaultEdition = _context.Editions.FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);
        if (defaultEdition == null)
        {
            defaultEdition = new Edition { Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName };
            _context.Editions.Add(defaultEdition);
            _context.SaveChanges();
        }

        if (defaultEdition.Id > 0)
        {
            CreateFeatureIfNotExists(defaultEdition.Id, AppFeatures.SimpleBooleanFeature, true);
        }
    }

    private void CreateFeatureIfNotExists(int editionId, string featureName, bool isEnabled)
    {
        var defaultEditionChatFeature = _context.EditionFeatureSettings
                                                    .FirstOrDefault(ef => ef.EditionId == editionId && ef.Name == featureName);

        if (defaultEditionChatFeature == null)
        {
            _context.EditionFeatureSettings.Add(new EditionFeatureSetting
            {
                Name = featureName,
                Value = isEnabled.ToString(),
                EditionId = editionId
            });
        }
    }
}